package com.hoshiicloud.lessee.controller.category;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.goods.entity.goods.PublicProperties;
import com.hoshiicloud.goods.param.goods.PublicPropertiesAddParam;
import com.hoshiicloud.goods.param.goods.PublicPropertiesQueryParam;
import com.hoshiicloud.goods.param.goods.PublicPropertiesUpdateParam;
import com.hoshiicloud.lessee.feign.LesseeGoodsFeignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 2019/8/8 16:44 qry
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/category/public-properties")
@Api(value = "品类属性-公共属性", tags = {"品类属性-公共属性"})
public class LesseePublicPropertiesController {
    private final LesseeGoodsFeignService goodsFeignService;
    public LesseePublicPropertiesController(LesseeGoodsFeignService goodsFeignService) {
        this.goodsFeignService = goodsFeignService;
    }

    @PostMapping("/getPage")
    @ApiOperation(value = "分页查询公共属性表")
    public Response<Page<PublicProperties>> page(@RequestBody @Valid PublicPropertiesQueryParam param, HttpServletRequest rq) {
        param.setCompanyId(UserInfoUtil.getUserInfo(rq).getCompanyId());
        return  goodsFeignService.getPublicPropertiesPage(param);
    }
    @PostMapping("/save")
    @ApiOperation(value = "新增公共属性表")
    public Response<Boolean> save(@RequestBody @Valid PublicPropertiesAddParam param, HttpServletRequest rq) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        param.setCompanyId(user.getCompanyId());
        param.setCreatedBy(user.getRealName());
        return goodsFeignService.savePublicProperties(param);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改公共属性表")
    public Response<Boolean> update(@RequestBody @Valid PublicPropertiesUpdateParam param, HttpServletRequest rq) {
        param.setUpdatedBy( UserInfoUtil.getUserInfo(rq).getRealName());
        return goodsFeignService.updatePublicProperties(param);
    }

    @GetMapping("/dropdownBox")
    @ApiOperation(value = "获取下拉框数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "propertyName",value = "属性名",dataType = "String",example = "参数类型")})
    public Response<List<String>> dropdownBox(@RequestParam("propertyName")  String propertyName, HttpServletRequest rq) {
        if(StringUtils.isBlank(propertyName)){
            return Response.failed("公共属性名不能为空");
        }
        Long companyId = UserInfoUtil.getUserInfo(rq).getCompanyId() ;
        return goodsFeignService.publicPropertiesDropdownBox(companyId,propertyName);
    }


}
